package com.atguigu.service.impl;

import com.atguigu.dao.UpdateTomcatCodeTaskDao;
import com.atguigu.dao.impl.UpdateTomcatCodeTaskDaoImpl;
import com.atguigu.pojo.Page;
import com.atguigu.pojo.UpdateTomcatCodeTask;
import com.atguigu.service.UpdateTomcatCodeTaskService;

import java.util.List;

public class UpdateTomcatCodeTaskServiceImpl implements UpdateTomcatCodeTaskService {
    UpdateTomcatCodeTaskDao updateTomcatCodeTaskDao=new UpdateTomcatCodeTaskDaoImpl();
    @Override
    public void addUpdateTomcatCodeTask(UpdateTomcatCodeTask updateTomcatCodeTask) {
        updateTomcatCodeTaskDao.addUpdateTomcatCodeTask(updateTomcatCodeTask);
    }

    @Override
    public void updateUpdateTomcatCodeTask(UpdateTomcatCodeTask updateTomcatCodeTask) {
        updateTomcatCodeTaskDao.updateUpdateTomcatCodeTask(updateTomcatCodeTask);
    }

    @Override
    public void deleteUpdateTomcatCodeTaskById(String id) {
        updateTomcatCodeTaskDao.deleteUpdateTomcatCodeTaskById(id);
    }

    @Override
    public UpdateTomcatCodeTask queryUpdateTomcatCodeTaskById(String id) {
        UpdateTomcatCodeTask updateTomcatCodeTask=updateTomcatCodeTaskDao.queryUpdateTomcatCodeTaskById(id);
        return updateTomcatCodeTask;
    }


    @Override
    public List<UpdateTomcatCodeTask> queryUpdateTomcatCodeTasks() {
        List<UpdateTomcatCodeTask> updateTomcatCodeTasks=updateTomcatCodeTaskDao.queryUpdateTomcatCodeTasks();
        return updateTomcatCodeTasks;
    }

    @Override
    public Page page(Integer pageNo, Integer pageSize) {
        Page<UpdateTomcatCodeTask> page=new Page<>();
        page.setPagesize(pageSize);
        Integer pageTotalCount=updateTomcatCodeTaskDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount % pageSize >0){
            pageTotal++;//总条数,除以一页条数,大于0,页码数+1
        }
        page.setPageTotal(pageTotal);
        System.out.println("pageNo 原来是 is:"+pageNo);
        if(pageNo<1){//防止用户输入量太大
            pageNo=1;
        }else if(pageNo>pageTotal){
            pageNo=pageTotal;
        }
        page.setPageNo(pageNo);//当前页码
        System.out.println("pageNo 现在是 is:"+pageNo);
        System.out.println("pageTotal is:"+pageTotal);
        //要得到的是page的items
        Integer begin=(page.getPageNo()-1)*pageSize;
        List<UpdateTomcatCodeTask> items=updateTomcatCodeTaskDao.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }
}
